package com.dx.hotel;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;

import com.dx.common.base.util.CommonConstants;
import com.dx.common.base.util.DateUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.mchange.v1.util.ArrayUtils;
import com.travelsky.hotelbesdk.cache.api.DefaultMultiHotelCacheAPI;
import com.travelsky.hotelbesdk.cache.api.MultiHotelCacheAPI;
import com.travelsky.hotelbesdk.cache.models.condition.DefaultMultiHotelCacheReqCondition;
import com.travelsky.hotelbesdk.models.easyhotel.multi.HotelRatePlan;
import com.travelsky.hotelbesdk.models.easyhotel.multi.HotelRoomRate;
import com.travelsky.hotelbesdk.models.easyhotel.multi.SingleHotel;
import com.travelsky.hotelbesdk.models.response.EasyMultiHotelResponse;
import com.travelsky.hotelbesdk.utils.Hotelbe_Biz_Exception;

public class InternalController  extends Controller{
	public void index() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {	
		//已登录则跳转到登陆成功页面
		if(this.getSession().getAttribute("user")!=null)
			redirect("/");
		else {
			MultiHotelCacheAPI mutilCache=new DefaultMultiHotelCacheAPI();
			DefaultMultiHotelCacheReqCondition condition=new DefaultMultiHotelCacheReqCondition();
			condition.setCheckInDate(DateUtil.getDateStr()); 
			condition.setCheckOutDate(DateUtil.getDateStrByAdd(CommonConstants.DATE_ADD_TYPE_DAY, 3)); 
			
			int currPage=1;
			if(!StringUtils.isBlank(String.valueOf(this.getPara("currPage"))) &&
					StringUtils.isNumeric(String.valueOf(this.getPara("currPage")))){
				currPage=Integer.parseInt(this.getPara("currPage"));
			}
			condition.setPageIndex(currPage);
			condition.setPageSize(5);			
			String queryCity=this.getPara("fromcity");
			
			if(!StringUtils.isBlank(queryCity)){			
				String sql="select city_code from hotel_city t where t.city_name='"+queryCity+"'";
				String cityCode=Db.queryStr(sql);
				if(!StringUtils.isBlank(cityCode)){
					condition.setCityCode(cityCode);
				}				
			}
			
			if(StringUtils.isBlank(condition.getCityCode()))
			{
				condition.setCityCode("WUH"); //城市代码 
			}
			
			
			try {
				long i=System.currentTimeMillis();
				EasyMultiHotelResponse respone=mutilCache.getMultiHotels(condition);
				System.out.println(System.currentTimeMillis()-i+" ms");
				System.out.println("总数量："+respone.getMultihotelinfo().getTotalhotels());
				//System.out.println("展示数量："+respone.getSinglehotel().size());
				if(respone.getMultihotelinfo().getTotalhotels()>0){
				for(SingleHotel singleHotel:respone.getSinglehotel()){
					//singleHotel.getBasichotelinfo().getMinrate()
					singleHotel.getRoomtypeinfo().get(0);
					System.out.println(singleHotel.getRoomtypeinfo().get(0).getRateplan().size());
					List<HotelRatePlan> list1=singleHotel.getRoomtypeinfo().get(0).getRateplan();
					for(int j=0;j<list1.size();j++){
						//list1.get(j).get
						System.out.println("综合信息:"+BeanUtils.describe(list1.get(j)));
						for(HotelRoomRate roomrate:list1.get(j).getRoomrate())
						{
							System.out.println(BeanUtils.describe(roomrate));
						}					
					}
				}
				}
				this.setAttr("currPage", currPage);
				this.setAttr("pageSize", respone.getMultihotelinfo().getPagesize());
				this.setAttr("pageCounts", respone.getMultihotelinfo().getTotalpage());
				this.setAttr("hotelList", respone.getSinglehotel());
				System.out.println("---"+respone.getSinglehotel().get(0).getRoomtypeinfo().get(1).getRoomtypecode());
				System.out.println("currPage:"+currPage);
			} catch (Hotelbe_Biz_Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			render("/WEB-INF/jsp/hotel/internal.jsp");
		}
	}
	
	public void queryCity(){
		String sql="select city_name from hotel_city t where t.city_pyfw like ? or  t.city_name like ? order by t.city_grade,t.city_pinyin";
		String queryValue=this.getPara("queryCity");
		JSONArray jsonArray1=new JSONArray();
		//queryValue="WUH";
		if(!StringUtils.isBlank(queryValue)){
			queryValue=queryValue.toUpperCase();
			Object[] params=new Object[2];
			params[0]=StringUtils.join(queryValue.split("|"), "%")+"%";
			params[1]=StringUtils.join(queryValue.split("|"), "%")+"%";
			List<String> list=Db.query(sql, params);
			System.out.println(ArrayUtils.toString(list.toArray()));
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("query", queryValue);
			JSONArray jsonArray2=new JSONArray();
			for (String s : list) {
				jsonArray1.add(s);
				jsonArray2.add(s);
			}
			jsonObject.put("suggestions", jsonArray1);
			
//			jsonArray.clear();
//			jsonArray.add("a");
//			jsonArray.add("b");
//			jsonArray.add("c");
//			jsonArray.add("d");
			jsonObject.put("data", jsonArray2);
			System.out.println(jsonObject.toString());	
			renderJson(jsonArray1);
		}else {
			renderJson(jsonArray1);
		}
	}
	
	public void test(){
		render("/WEB-INF/jsp/test/auto.jsp");
	}
}
